﻿using System.Collections.Generic;

namespace HREngine.Bots
{
    public partial class Behavior骑士 : Behavior
    {
        public override string BehaviorName() { return "骑士"; }


        // 发现卡的价值
        public override int getDiscoverVal(CardDB.Card card, Playfield p)
        {
            switch (card.chnName)
            {
                case "定罪（等级1）": 
                    if(p.ownMinions.Count > 2)
                        return 100;
                    return 10;
                case "威能祝福":
                case "正义圣契":
                case "阿达尔之手":
                case "王者祝福":
                case "智慧圣契":
                case "新生入学":
                    return 5;
                case "奉献":
                    return 3;
                case "流光之赐":
                case "荣誉护盾":
                case "开赛集结":
                case "路障":
                case "游园日":
                case "惩黑除恶":
                case "动员布道":
                    return 2;
                case "零食大冲关":
                    return 1;

            }
            return 0;
        }

        public override int getComboPenality(CardDB.Card card, Minion target, Playfield p)
        {
            // 无法选中
            if (target != null && target.untouchable)
            {
                return 100000;
            }
            // 初始惩罚值
            int pen = 0;
            // 腐蚀刀
            foreach(Handmanager.Handcard hc in p.owncards)
            {
                if(hc.card.Corrupt && card.cost > hc.manacost)
                {
                    pen -= 10;
                    break;
                }
            }
            int bonus = 4;
            switch (card.chnName)
            {
                case "幸运币":
                    pen += bonus * 2;
                    break;
                //-----------------------------超模补正-----------------------------------
                case "蛛魔之卵":
                    pen -= bonus ;
                    break;
                case "活化扫帚":
                    pen -= bonus * 3;
                    break;
                case "审判圣契":
                    if (!card.Corrupt && p.ownHero.Hp < 15) pen -= 20;
                    if (!p.ownHero.Ready)
                        pen += 20;
                    break;
                case "剧毒魔蝎":
                    pen -= bonus * 3;
                    break;
                case "泰兰·弗丁":
                    pen -= bonus * 3;
                    break;
                case "剑圣萨穆罗":
                    pen += bonus * 7;
                    break;
                case "提里奥·弗丁":
                    pen -= bonus * 5;
                    break;
                // 异能价值 2 属性
                case "苦痛侍僧":
                case "战利品贮藏者":
                case "麦田傀儡":
                    pen -= bonus * 2;
                    break;
                case "莫戈尔·莫戈尔格":
                case "双盾优等生":
                case "曼科里克":
                case "月牙":
                case "银色侍从":
                case "灵魂之匣":
                case "凯恩·血蹄":
                    pen -= bonus * 2;
                    break;
                case "希望圣契":
                    pen -= bonus * 3;
                    break;
                //-----------------------------武器-----------------------------------
                case "逝者之剑":
                    break;
                case "正义圣契":
                    pen += 20;
                    break;
                //-----------------------------针对卡-----------------------------------
                case "异教低阶牧师":
                    if (p.enemyHeroStartClass == TAG_CLASS.MAGE || p.enemyHeroStartClass == TAG_CLASS.PRIEST) pen -= 20;
                    break;
                case "食人魔巫术师":
                    if (p.enemyMinions.Count == 0 && p.ownMinions.Count < 4)
                        pen -= 9;
                    pen -= 30;
                    break;
                case "古神在上":
                    if (p.ownMaxMana <= 3) pen += 4;
                    pen -= 3;
                    break;
                //-----------------------------配合-----------------------------------
                case "十字路口大嘴巴":
                    pen -= 10 * p.ownSecretsIDList.Count;
                    break;
                case "夺日者间谍":
                    pen += p.ownSecretsIDList.Count > 0 ? -bonus * 2 : bonus;
                    break;
                case "银色自大狂":
                    pen += 4 * bonus;
                    break;
                case "前沿哨所":
                    // 前两个回合优先下，赌对面解不掉
                    if (p.ownMaxMana <= 2) return -100;
                    if (p.enemyHeroStartClass == TAG_CLASS.MAGE || p.enemyHeroStartClass == TAG_CLASS.PRIEST)
                        return -15;
                    return -10;
                case "防护长袍":
                    if (p.enemyHeroStartClass == TAG_CLASS.MAGE || p.enemyHeroStartClass == TAG_CLASS.PRIEST)
                        return -30;
                    return 0;
                case "火炮长斯密瑟":
                    pen -= 20 * p.ownSecretsIDList.Count;
                    break;
                case "钉棍终结者":
                    pen += p.ownSecretsIDList.Count > 0 && p.enemyMinions.Count > 0 ? -20 : 30;
                    break;
                case "北卫军指挥官":
                    pen += p.ownSecretsIDList.Count > 0 ? -20 : 1;
                    break;
                case "复仇":
                    return -1;
                //-----------------------------英雄技能-----------------------------------
                case "援军":
                    return 9;
            }
            if (card.type == CardDB.cardtype.SPELL)
            {
                pen -= 2;
            }
            return pen;
        }

        private static int costVal = 10;
        // 抽牌价值
        private static int drawCardVal = 10;
        
        PenalityManager penman = PenalityManager.Instance;

        // 核心，场面值

        public override float getPlayfieldValue(Playfield p)
        {
            if (p.value > -20000) return p.value;
            float retval = 0;
            retval += getGeneralVal(p);
            // 圣契奖励
            retval += p.libram > 2 ? 60 + p.libram * 10 : p.libram * 30;
            // 危险血线
            int hpboarder = 15;
            // 抢脸血线
            int aggroboarder = 12;
            retval += getHpValue(p, hpboarder, aggroboarder);
            // 对手基本随从交换模拟
            retval += enemyTurnPen(p);
            // 出牌序列数量
            int count = p.playactions.Count;
            int ownActCount = 0;
            // 排序问题！！！！
            for (int i = 0; i < count; i++)
            {
                Action a = p.playactions[i];
                ownActCount++;
                switch (a.actionType)
                {
                    // 英雄攻击
                    case actionEnum.attackWithHero:
                    case actionEnum.attackWithMinion:
                        if(a.target != null && a.target.handcard.card.chnName == "前沿哨所")
                        {
                            retval -= i * 5;
                        }
                        continue;
                    case actionEnum.useHeroPower:
                        continue;
                    case actionEnum.playcard:
                        break;
                    default:
                        continue;
                }
                switch (a.card.card.chnName)
                {
                    // 排序优先
                    case "圣礼骑士":
                    case "阿达尔之手":
                    case "新生入学":
                        retval -= i * 2;
                        break;
                    case "恩佐斯，深渊之神":
                        retval += i;
                        break;
                }
            }
            foreach (Handmanager.Handcard hc in p.owncards)
            {
                // 如果手上有侏儒就很赚
                if (hc.card.chnName == "甩笔侏儒")
                {
                    retval += 10;
                }
            }
            retval += getSecretPenality(p); // 奥秘的影响
            //p.value = retval;
            return retval;
        }

        // 敌方随从价值 主要等于（HP + Angr） * 4  + 5
        public override int getEnemyMinionValue(Minion m, Playfield p)
        {
            bool dieNextTurn = false;
            foreach (Minion mm in p.enemyMinions)
            {
                if (mm.handcard.card.chnName == "末日预言者")
                {
                    dieNextTurn = true;
                    break;
                }
            }
            if (m.destroyOnEnemyTurnEnd || m.destroyOnEnemyTurnStart || m.destroyOnOwnTurnEnd || m.destroyOnOwnTurnStart) dieNextTurn = true;
            if (dieNextTurn)
            {
                return -1;
            }
            if (m.Hp <= 0) return 0;
            int retval = 1;
            if(m.Angr> 0 || p.enemyHeroStartClass == TAG_CLASS.PALADIN || p.enemyHeroStartClass == TAG_CLASS.PRIEST)
                retval += m.Hp * 4;
            // 法强相当于 1.5 点属性
            retval += m.spellpower * 6;
            if (!m.frozen && (!m.cantAttack || m.handcard.card.chnName == "邪刃豹") )
            {
                retval += m.Angr * 4;
                if (m.windfury) retval += m.Angr * 2;
            }
            if (m.silenced) return retval;

            if (m.taunt) retval += 2;
            if (m.divineshild) retval += m.Angr * 2;
            if (m.divineshild && m.taunt) retval += 5;
            if (m.stealth) retval += 2;

            // 剧毒价值两点属性
            if (m.poisonous)
            {
                retval += 8;
            }
            if (m.lifesteal) retval += m.Angr * 2;

            int bonus = 4;
            switch (m.handcard.card.chnName){
                case "血骨傀儡":
                    return 100;
                // 异能价值每多 1 属性值加权 4 点
                // 异能价值 4 属性值
                case "原野联络人":
                case "奥":
                case "奥的灰烬":
                case "科卡尔驯犬者":
                case "巡游领队":
                case "团伙核心":
                case "前沿哨所":
                case "莫尔杉哨所":
                case "塔姆辛·罗姆":
                case "导师火心":
                case "伊纳拉·碎雷":
                case "暗影珠宝师汉纳尔":
                case "伦萨克大王":
                case "洛卡拉":
                    retval += bonus * 4;
                    break;
                // 异能价值 2 属性值
                case "萨特监工":
                case "食人魔巫术师":
                case "甩笔侏儒":
                case "精英牛头人酋长，金属之神":
                case "克欧雷":
                case "雷欧克":
                case "纳兹曼尼织血者":
                case "塞泰克织巢者":
                    retval += bonus * 2;
                    break;
                // 异能价值 2 属性值
                case "新生刺头":
                case "聒噪怪":
                    if(m.Spellburst)
                        retval += bonus * 2;
                    break;
            }
            return retval;
        }


        public override int getSirFinleyPriority(List<Handmanager.Handcard> discoverCards)
        {
            
            return -1; //comment out or remove this to set manual priority
            int sirFinleyChoice = -1;
            int tmp = int.MinValue;
            for (int i = 0; i < discoverCards.Count; i++)
            {
                CardDB.cardName name = discoverCards[i].card.name;
                if (SirFinleyPriorityList.ContainsKey(name) && SirFinleyPriorityList[name] > tmp)
                {
                    tmp = SirFinleyPriorityList[name];
                    sirFinleyChoice = i;
                }
            }
            return sirFinleyChoice;
        }
        // 我方随从价值，大致等于主要等于 （HP + Angr） * 4  + 5
        public override int getMyMinionValue(Minion m, Playfield p)
        {
            bool dieNextTurn = false;
            foreach (Minion mm in p.enemyMinions)
            {
                if (mm.handcard.card.chnName == "末日预言者")
                {
                    dieNextTurn = true;
                    break;
                }
            }
            if (m.destroyOnEnemyTurnEnd || m.destroyOnEnemyTurnStart || m.destroyOnOwnTurnEnd || m.destroyOnOwnTurnStart) dieNextTurn = true;
            if (dieNextTurn)
            {
                return -1;
            }
            if (m.Hp <= 0) return 0;
            int retval = 1;
            if (m.Hp <= 0) return 0;
            retval += m.Hp * 4;
            retval += m.Angr * 4;
            if (m.Hp <= 1) retval -= (m.Angr - 1) * 3;
            if (m.Angr <= 1)
            {
                if(m.Hp > 3)
                {
                    retval -= (m.Hp -3)  * 4;
                }
            }
            // 高攻低血是垃圾
            if (m.Angr > m.Hp + 4) retval -= (m.Angr - m.Hp) * 3;
            
            // 风怒价值
            if ((!m.playedThisTurn || m.rush == 1 || m.charge == 1) && m.windfury) retval += m.Angr;
            // 圣盾价值
            if (m.divineshild) retval += m.Angr * 3;
            // 潜行价值
            if (m.stealth) retval += m.Angr / 2 + 1;
            // 吸血
            if (m.lifesteal) retval += m.Angr / 2 + 1;
            // 圣盾嘲讽
            if (m.divineshild && m.taunt) retval += 4;

            int bonus = 4;
            switch (m.handcard.card.chnName)
            {
                
                case "蛛魔之卵":
                    if (m.Angr > 0)
                        retval += bonus * 3;
                    break;
                // 异能价值 4 属性
                case "炎魔之王拉格纳罗斯":
                case "伊瑟拉":
                    retval += bonus * 4;
                    break;
            }

            return retval;
        }

        private Dictionary<CardDB.cardName, int> SirFinleyPriorityList = new Dictionary<CardDB.cardName, int>
        {
            //{HeroPowerName, Priority}, where 0-9 = manual priority
            { CardDB.cardName.lesserheal, 0 }, 
            { CardDB.cardName.shapeshift, 6 },
            { CardDB.cardName.fireblast, 7 },
            { CardDB.cardName.totemiccall, 1 },
            { CardDB.cardName.lifetap, 9 },
            { CardDB.cardName.daggermastery, 5 },
            { CardDB.cardName.reinforce, 4 },
            { CardDB.cardName.armorup, 2 },
            { CardDB.cardName.steadyshot, 8 }
        };
        public override int getSirFinleyPriority(CardDB.Card card)
        {
            return SirFinleyPriorityList[card.name];
        }


        public override int getHpValue(Playfield p, int hpboarder, int aggroboarder)
        {
            int retval = 0;
            // 血线安全
            if (p.ownHero.Hp + p.ownHero.armor > hpboarder)
            {
                retval += (5 + p.ownHero.Hp + p.ownHero.armor - hpboarder) * 2;
            }
            // 快死了
            else
            {
                if (p.nextTurnWin()) retval -= (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor) * 2;
                else retval -= 5 * (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor) ;
            }
            if (p.ownHero.Hp + p.ownHero.armor < 10 && p.ownHero.Hp + p.ownHero.armor > 0)
            {
                retval -= 200 / (p.ownHero.Hp + p.ownHero.armor);
            }
            // 对手血线安全
            if (p.enemyHero.Hp + p.enemyHero.armor >= aggroboarder)
            {
                retval += (aggroboarder - p.enemyHero.Hp - p.enemyHero.armor) * 3;
            }
            // 开始打脸
            else 
            {
                retval += 7 * (aggroboarder + 1 - p.enemyHero.Hp - p.enemyHero.armor);
            }
            // 进入斩杀线
            if (p.enemyHero.Hp + p.enemyHero.armor <= 5)
            {
                retval += 50;
            }            
            return retval;
        }

        /// <summary>
        /// 攻击触发的奥秘惩罚
        /// </summary>
        /// <param name="si"></param>
        /// <param name="attacker"></param>
        /// <param name="defender"></param>
        /// <returns></returns>
        public override int getSecretPen_CharIsAttacked(Playfield p, SecretItem si, Minion attacker, Minion defender)
        {
            if (attacker.isHero) return 0;
            int pen = 0;
            // 攻击的基本惩罚
            if (si.canBe_explosive && !defender.isHero)
            {
                pen -= 20;
                //pen += (attacker.Hp + attacker.Angr);
                foreach (SecretItem sii in p.enemySecretList)
                {
                    sii.canBe_explosive = false;
                }
            }
            return pen;
        }

    }
}